package com.frontsurf.dao;

import com.frontsurf.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author: hushengjin
 *
 * JpaRepository<实体类类型，主键类型>：用来完成基本CRUD操作
 * JpaSpecificationExecutor<实体类类型>：用于复杂查询（分页等查询操作）
 */
public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {

    @Query(value = "from Customer ")
    List<Customer> findAllCustomer();

    @Query(value = "from Customer where custName=?1")
    Customer findCustomer(String custName);

    @Query(value = "update Customer set custAddress=?1 where custId=?2")
    @Modifying
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    void updateCustomer(String custAddress, Long custId);

    /**
     * nativeQuery : 使用本地sql的方式查询
     *
     * @return
     */
    @Query(value = "select * from cst_customer", nativeQuery = true)
    List<Customer> findSql();

    Customer findByCustNameAndCustId(String name, Long id);

    @Query(value = "from Customer where custAddress=?2 and custId=?1")
    Customer findByCustAddressAndCustId(Long id, String name);
}
